//
//  HMEveryDayViewModel.m
//  HowToDo
//
//  Created by 胡坤 on 2017/1/6.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMEveryDayViewModel.h"
#import "HMNetWorkTool.h"
#import "HMListenAnyTimeResModel.h"
#import "HMEVDateGroup.h"
#import <YYModel.h>

@interface HMEveryDayViewModel ()

/**
 
 */
@property(nonatomic,strong)NSMutableArray* sectionGroupList;

/**
 时间点标记
 */
@property(copy,nonatomic)NSString* currentTimStr;



@end

@implementation HMEveryDayViewModel

-(void)downLoadMp3FileWithFreeAudio:(HMListenAnyTimeResModel *)freeAudio progress:(void(^)(float progress))progress completion:(void(^)(NSString* filePath))completion
{
    [[DownloadManager sharedManager]downloadWithURLString:freeAudio.audio_file_url progress:progress completion:completion];
    
}



//请求天天长知识数据的方法
-(void)requestFreeAudioListFinished:(void(^)(NSError *error))finishedBlock
{
    NSLog(@"%@", @"dd");
    [[HMNetWorkTool sharedTool]requestFreeAudioFinished:^(id responseObject)
    {
        if ([responseObject isKindOfClass:[NSDictionary class]])
        {
            NSArray * dataArr = responseObject[@"data"];
            
            NSArray<HMListenAnyTimeResModel*>* modelList = [NSArray yy_modelArrayWithClass:[HMListenAnyTimeResModel class] json:dataArr];
            
            //升序排序以后
            NSMutableArray* temp1 = [NSMutableArray array];
            
            [modelList enumerateObjectsUsingBlock:^(HMListenAnyTimeResModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
            {
                //获取当前时间
                NSDate* date = [NSDate dateWithTimeIntervalSince1970:obj.add_time.longLongValue];
                
                NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
                
                [formatter setDateFormat:@"YYYY-MM-dd"];
                
                NSString* timeStr = [formatter stringFromDate:date];
                
                if (idx == 0)
                {
                    [temp1 addObject:timeStr];
                    self.currentTimStr = timeStr;
                }
                
                if ([self.currentTimStr isEqualToString:timeStr])
                {
                    return ;
                }else
                {
                    self.currentTimStr = timeStr;
                    [temp1 addObject:timeStr];
                }
            }];
            
            [temp1 sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2)
            {
                NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                
                [formatter setDateFormat:@"YYYY-MM-dd"];
                
                if (obj1 == [NSNull null]) {
                    obj1 = @"0000/00/00";
                }
                if (obj2 == [NSNull null]) {
                    obj2 = @"0000/00/00";
                }
                NSDate *date1 = [formatter dateFromString:obj1];
                NSDate *date2 = [formatter dateFromString:obj2];
                NSComparisonResult result = [date1 compare:date2];
                //降序排列
                return result == NSOrderedAscending;
            }];
            
            NSArray *newList = temp1.copy;
            
            [newList enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop)
            {
                HMEVDateGroup *dateGroup = [[HMEVDateGroup alloc]init];
                 dateGroup.dayTime = obj;
                [self.sectionGroupList addObject:dateGroup];
            }];
            
            for (HMEVDateGroup *dateGroup in self.sectionGroupList)
            {
                NSMutableArray * temp = [NSMutableArray array];
                
                [modelList enumerateObjectsUsingBlock:^(HMListenAnyTimeResModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
                 {
                     //获取当前时间
                     NSDate* date = [NSDate dateWithTimeIntervalSince1970:obj.add_time.longLongValue];
                     NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
                     [formatter setDateFormat:@"YYYY-MM-dd"];

                     NSString* timeStr = [formatter stringFromDate:date];
                     [formatter  setDateFormat:@"hh:mm"];
                     NSString* hourTimstr = [formatter stringFromDate:date];
                     if ([dateGroup.dayTime isEqualToString:timeStr])
                     {
                         obj.hourTime = hourTimstr;

                         [temp addObject:obj];
                     }
                 }];
                dateGroup.freeAudioList = temp.copy;
            }
            __weak typeof(self)weakSelf = self;
            self.freeaudioList = weakSelf.sectionGroupList.copy;
        
        }
        finishedBlock(nil);
        
    } Failed:^(NSError *error)
    {
        finishedBlock(error);
    }];
}

-(NSMutableArray *)sectionGroupList
{
    if (!_sectionGroupList)
    {
        _sectionGroupList = [NSMutableArray array];
    }
    return _sectionGroupList;
}

- (NSArray<HMEVDateGroup *> *)freeaudioList
{
    if (!_freeaudioList)
    {
        _freeaudioList = [NSArray array];
    }
    return _freeaudioList;
}
@end
